﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
}

<link href="~/css/site.css" rel="stylesheet" />
<link href="~/layui/css/layui.css" rel="stylesheet" />

<div class="layui-container" style="padding:20px">
    <div class="layui-row">
        <div class="layui-col-md12">
            <h2>开票详情</h2>
        </div>
    </div>

    <div class="layui-row">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">基础信息</div>
                <div class="layui-card-body">
                    <div class="layui-form layui-form-pane">
                        <div class="layui-form-item">
                            <label class="layui-form-label">开票ID</label>
                            <div class="layui-input-block">
                                <input type="text" id="openFareId" class="layui-input" disabled>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">开票编号</label>
                            <div class="layui-input-block">
                                <input type="text" id="openFareCode" class="layui-input" disabled>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">开票主题</label>
                            <div class="layui-input-block">
                                <input type="text" id="openFareTheme" class="layui-input" disabled>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">发票类型</label>
                            <div class="layui-input-block">
                                <input type="text" id="invoiceTypeName" class="layui-input" disabled>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">开票日期</label>
                            <div class="layui-input-block">
                                <input type="text" id="openFareTime" class="layui-input" disabled>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">收票方</label>
                            <div class="layui-input-block">
                                <input type="text" id="collectTickets" class="layui-input" disabled>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">开票金额</label>
                            <div class="layui-input-block">
                                <input type="text" id="openFareMoney" class="layui-input" disabled>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">发票号</label>
                            <div class="layui-input-block">
                                <input type="text" id="invoiceTypeCode" class="layui-input" disabled>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">备注</label>
                            <div class="layui-input-block">
                                <textarea id="openFareRemark" class="layui-textarea" disabled></textarea>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div style="margin-top: 10px;">
                <button type="button" class="layui-btn layui-btn-warm" id="backToList">
                    <i class="layui-icon layui-icon-left"></i> 返回列表
                </button>
                <button type="button" class="layui-btn" id="goEdit">
                    <i class="layui-icon layui-icon-edit"></i> 去修改
                </button>
            </div>
        </div>
    </div>
</div>

<!-- 先引入jQuery，再引入其他脚本（如页面单独使用，可保留；若全局已引入，可不重复） -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="~/layui/layui.js"></script>

<script>
    $(function () {
        var openFareId = getOpenFareId();
        if (!openFareId) {
            msgWarn('未获取到开票ID，无法加载详情');
            return;
        }
        loadDetails(openFareId);

        $('#backToList').on('click', function () {
            window.location.href = '/OpenFare/ShowOpenFare';
        });
        $('#goEdit').on('click', function () {
            localStorage.setItem('openFareId', openFareId);
            window.location.href = '/OpenFare/UpdateOpenFare/' + openFareId;
        });
    });

    function msgInfo(text) { if (window.layui && layui.layer) { layui.layer.msg(text); } else { alert(text); } }
    function msgWarn(text) { if (window.layui && layui.layer) { layui.layer.msg(text); } else { alert(text); } }

    function getOpenFareId() {
        // 1) localStorage
        var id = localStorage.getItem('openFareId');
        if (id) return id;
        // 2) querystring ?id=xx 或 ?OpenFareId=xx
        var params = new URLSearchParams(window.location.search);
        id = params.get('id') || params.get('OpenFareId');
        if (id) return id;
        // 3) URL 最后一段是数字则采用
        var segs = window.location.pathname.split('/').filter(Boolean);
        if (segs.length > 0) {
            var last = segs[segs.length - 1];
            if (/^\d+$/.test(last)) return last;
        }
        return null;
    }

    function loadDetails(openFareId) {
        $.ajax({
            url: 'http://localhost:5054/api/OpenFare/FanTianOpenFare?OpenFareId=' + encodeURIComponent(openFareId),
            dataType: 'json',
            type: 'get',
            contentType: 'application/json',
            success: function (res) {
                if (res && (res.code === 200 || res.Code === 200)) {
                    var data = res.data;
                    if (Array.isArray(data) && data.length > 0) data = data[0];
                    fillForm(data);
                    msgInfo('加载成功');
                } else {
                    msgWarn('加载失败：' + (res && (res.msg || res.Message) || '未知错误'));
                }
            },
            error: function (xhr, status, error) {
                msgWarn('请求失败：' + (xhr.responseJSON && (xhr.responseJSON.title || JSON.stringify(xhr.responseJSON.errors)) || error));
            }
        });
    }

    function fillForm(d) {
        if (!d) return;
        $('#openFareId').val(d.openFareId || d.id || '');
        $('#openFareCode').val(d.openFareCode || '');
        $('#openFareTheme').val(d.openFareTheme || '');
        $('#invoiceTypeName').val(d.invoiceTypeName || '');
        $('#openFareTime').val(formatDate(d.openFareTime));
        $('#collectTickets').val(d.collectTickets || '');
        $('#openFareMoney').val(d.openFareMoney || '');
        $('#invoiceTypeCode').val(d.invoiceTypeCode || '');
        $('#openFareRemark').val(d.openFareRemark || '');
    }

    function formatDate(val) {
        if (!val) return '';
        // 兼容 ISO 字符串或时间戳
        try {
            var dt = new Date(val);
            if (isNaN(dt.getTime())) return val;
            var y = dt.getFullYear();
            var m = String(dt.getMonth() + 1).padStart(2, '0');
            var d = String(dt.getDate()).padStart(2, '0');
            var hh = String(dt.getHours()).padStart(2, '0');
            var mm = String(dt.getMinutes()).padStart(2, '0');
            var ss = String(dt.getSeconds()).padStart(2, '0');
            return y + '-' + m + '-' + d + ' ' + hh + ':' + mm + ':' + ss;
        } catch { return val; }
    }
</script>